﻿#include "stdafx.h"
#ifdef LK_
namespace LK
{
	std::ostream& operator<<(std::ostream&o, ptr<Object> &x)	{
		return o << x->ToString();
	}
	std::wostream& operator<<(std::wostream&o, ptr<Object> &x)	{
		return o << x->ToString();
	}
	std::ostream& operator<<(std::ostream&o, Object const&x)	{
		return o << x.ToString();
	}
	std::wostream& operator<<(std::wostream&o, Object const&x){
		return o << x.ToString();
	}
	PType Object::GetType()const { return Type::Make<PObject>();
	}
	void Object::SwapWith(PObject& x){
		if (GetType() != x->GetType())Throw<PInvalidArgumentException>(L"无效参数：指定的交换对象与当前对象类型不一致");
	}
	bool Object::ReferenceEqual(PObject t1,PObject t2){
		return t1.pObject == t2.pObject; }
	void Object::ChangePtr(ptr<Object>&r, Object*p) { r.SetTo(p); }
	void Object::AddTypeInfo(){
#ifdef LK_REFLECTION_
		//引用反射命名空间
		using namespace Reflection;
		//获取类库的引用
		ptr<TypeLibrary> lib = GetGlobleLibrary();
		//获取类库中的全局命名空间
		PNamespaceNode globle = lib.GetGlobleNamespaceRef();
		//添加LK命名空间
		PNamespaceNode space = globle.FindOrAddNamespace(L"LK");
		//在LK命名空间中添加ptr<Object>类型
		PTypeNode n = space.FindOrAddTypeNode(L"Object", Type::Make<ptr<Object>>());
		//在ptr<Object>类型节点中添加成员函数
		n.AddFunction(L"ReferenceEqual", &Object::ReferenceEqual);
		n.AddFunction(L"ToString", &fref<PString(Object::*)()const,&Object::ToString>::operator());
#endif
	}
	//输出该对象的字符串描述
	ptr<String> Object::ToString()const {
		return GetType()->GetFullName();
	}
	bool Object::Equals(PObject const &obj)const { return this == obj.GetPtr(); }
	/**
	@brief 判断两个代理对象是否相等*/
	bool Object::Equals(PObject t1, PObject t2) { if (t1.pObject == t2.pObject)return true; if (t1.UseCount <= 0 || t2.UseCount <= 0)return false; return t1.pObject->Equals(t2); }

}
#endif
